package com.ithiema.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ithiema.reggie.Service.ShoppingCartService;
import com.ithiema.reggie.common.BaseContext;
import com.ithiema.reggie.common.R;
import com.ithiema.reggie.entity.ShoppingCart;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    //购物车
    //根据id查看
    @GetMapping("/list")
    private R<List<ShoppingCart>> list(){
        log.info("查看购物车...");
        LambdaQueryWrapper<ShoppingCart> lwp = new LambdaQueryWrapper<>();
        //  BaseContext.getCurrentId() 获取前登陆用户查询
        lwp.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        //根据创建时间倒叙排序
        lwp.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lwp);
        return R.success(list);
    }

    //添加购物车
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("接收到添加购物车的请求:{}", shoppingCart);
        //获取当前用户的id
        long currentId = BaseContext.getCurrentId();
        //1. 查询当前用户的购物车中是否已经有该 菜品或者套餐
        LambdaQueryWrapper<ShoppingCart> lwp = new LambdaQueryWrapper<>();
        //查人  因为可能登录的不是同一个的账号
        lwp.eq(ShoppingCart::getUserId,currentId);
        //查询菜品
        lwp.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        //查询套餐
        lwp.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        ShoppingCart one = shoppingCartService.getOne(lwp);

        //需要判断了  {商品数量}
        if (one!=null){
            //修改商品数量
            one.setNumber(one.getNumber()+1);
            //修改当前类
            shoppingCartService.updateById(one);
            return R.success(one);
        }else {
            //3. 如果没有？添加
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(currentId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }


    }


    @DeleteMapping("/clean")
    private R<String> clean(){
        //根据当前登陆用户，删除购物车数据
        long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lwp = new LambdaQueryWrapper<>();
        lwp.eq(ShoppingCart::getUserId,currentId);
        shoppingCartService.remove(lwp);
        return R.success("已清空");
    }


    //删除购物车的东西
    @PostMapping("/sub")
    private R<String> del(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> lwp = new LambdaQueryWrapper<>();
        //确定当前用户.并且不可以为空
        lwp.eq(shoppingCart.getUserId()!=null,ShoppingCart::getUserId,shoppingCart.getUserId());
        //确定和菜品id是否一样
        lwp.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        //确定和套餐id是否一样
        lwp.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        //根据条件查询 是1直接删除 , 不是1的话-1
            //获取数据,这样可以获取到数据库里的每个信息
        ShoppingCart one = shoppingCartService.getOne(lwp);
        if (one.getNumber()==1){
            shoppingCartService.remove(lwp);
            return R.success("删除成功");
        }else {
            //这里直接设置 num==获取的num-1
            one.setNumber(one.getNumber()-1);
            //把减少数量后的商品对象，修改进数据表
            shoppingCartService.updateById(one);
            return R.success("减少成功");
        }

    }
}
